import { createRouter, createWebHistory, type RouteLocationNormalized } from 'vue-router';
import routes from './routes';
import { useCachedViewStoreHook } from '@/store/modules/cachedView';
// import NProgress from '@/utils/progress';
import setPageTitle from '@/utils/set-page-title';
import store from '@/utils/store';

const router = createRouter({
    history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
    routes
});

export interface toRouteType extends RouteLocationNormalized {
    meta: {
        title?: string;
        noCache?: boolean;
    };
}

router.beforeEach((to: toRouteType, from, next) => {
    // NProgress.start();
    // 路由缓存
    useCachedViewStoreHook().addCachedView(to);
    // 页面 title
    setPageTitle(to.meta.title);
    store.set('PAGE_ID', to.meta.pageId);
    next();
});

router.afterEach(() => {
    // NProgress.done();
});

export default router;
